/**
 * @author shahma
 */
dojo.provide("medryx.orm.Collection");
dojo.require("medryx.orm.Association");
dojo.require("dojox.collections.ArrayList");
dojo.require("medryx.exceptions.IllegalArgumentException");

/**
 *  ARGUMENTS:
 *  readOnly: NOT IMPLEMENTED. ALL COLLECTIONS ARE READONLY FOR NOW
 *  lazy: TRUE | false //if false then if this property is not initialized at object creation time, it is fetched immediately. o
 * 			  otherwise, it is only fetched when a call to the property's accessor is made
 *  collectionType:"ARRAY" (uses primitive array, and is only valid for readonly = true), otherwise
 */
dojo.declare("medryx.orm.Collection", medryx.orm.Association, {
	
	
	
	initializeArray:function(value) {
		var array = [];
		dojo.forEach(value, function(item, index) {
			if (item) {
				var entity = this.getValueToAssign(item);
				array[index] = entity;
			} else {
				throw new medryx.exceptions.IllegalArgumentException("each value in array must be non-null/non-zero");
			}
		}, this);
		
		return array;
	},
	
    initialize:function(instance, propertyName, value) {
		var propertyValue = null;
		if (typeof value === "undefined") {
			propertyValue = this;
		} else if (dojo.isArrayLike(value) || value === null) {
			propertyValue = this.initializeArray(value);
		} else if (dojo.isString(value)) {
			//handle as csv
			var array = value.split(/\s*?,\s*?/); //splits and trims in one shot
			propertyValue = this.initializeArray(array);
			
		} else {
			throw new medryx.exceptions.IllegalArgumentException("value must be an array or null");
		}
        this.set(instance, propertyName, propertyValue);
		return instance[propertyName];
    }
});
